package cn.itsource.basic.service.impl;

import cn.itsource.basic.mapper.BaseMapper;
import cn.itsource.basic.query.Basequery;
import cn.itsource.basic.service.IBaseService;
import cn.itsource.basic.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T>{
    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    public void save(T t) {
        baseMapper.save(t);
    }

    @Override
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public void delete(Long id) {
        baseMapper.delete(id);
    }

    @Override
    public T findById(Long id) {
        return baseMapper.findById(id);
    }

    @Override
    public List<T> findAll() {
        return baseMapper.findAll();
    }

    @Override
    public PageInfo<T> findAllByQuery(Basequery basequery) {
        //查询总条数
        Long total = baseMapper.findTotal(basequery);
        //判断条数是否为空
        if(total.equals(0L)){
            return new PageInfo<>();
        }
        //查询分页列表
        List<T> list = baseMapper.findAllByQuery(basequery);
        return new PageInfo<>(list,total);
    }

    @Override
    public void batchDelete(List<T> list) {
        baseMapper.batchDelete(list);
    }
}
